package com.cskaoyan.market.controller;

import com.cskaoyan.market.service.AdminRegionService;
import com.cskaoyan.market.service.AdminRegionServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.MarketRegionVo;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/region/list")
public class AdminRegionController extends HttpServlet {
    AdminRegionService adminRegionService = new AdminRegionServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext=getServletContext();
        List<MarketRegionVo> marketRegionVoList= (List<MarketRegionVo>) servletContext.getAttribute("area");
        if (marketRegionVoList==null){
            marketRegionVoList=adminRegionService.list();
            servletContext.setAttribute("area",marketRegionVoList);
        }
        Object ok= ResponseUtil.okList(marketRegionVoList);
        String s= JacksonUtil.getObjectMapper().writeValueAsString(ok);
        resp.getWriter().write(s);
    }
}
